/**
 * Created by Administrator on 2017/5/17.
 */


$(function(){
    camaraTree();
    showToolbar = 1;
    initPlugin();
});

function shishi(obj){
    playMode=0;
    $("#camaraList").css("height","100%");
    $("#timediv").css("display","none");
    $('#shishi').css("background-color","#3366FF");
    $('#huifang').css("background-color","");
    uninit();
    layout = "2x2";
    init();
}
function huifang(){
    playMode=1;
    var nowDate = new Date();
    var year = nowDate.getFullYear();
    var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1)
        : nowDate.getMonth() + 1;
    var day = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate
        .getDate();
    var dateStr = year + "-" + month + "-" + day;
    $("#beginTime").val(dateStr+" 00:00:00");
    $("#endTime").val(dateStr+" 23:59:59");
    $("#camaraList").css("height","74%");
    $("#timediv").css("display","block")
    $('#huifang').css("background-color","#3366FF");
    $('#shishi').css("background-color","");
    uninit();
    layout = "1x1";
    init();
}

 var setting = {
     view: {
         fontCss : {color:"white"},
     },
     data: {
         simpleData: {
             enable: true,
             idKey: "id",
             pIdKey: "parentId",
             rootPId: -1
         },
         key: {
             url:"nourl"
         }
     },
     /* check: {
          enable: true ,//显示复选框
          chkStyle : "radio"
      },*/
     callback:{
         onClick: click// 点击事件回调函数
     }
 };
var ztree;

function click(event, treeId, treeNode){
    if(treeNode.isClick){
        $("#cName").html("当前监控点："+treeNode.name);
        $("#cName").attr("name",treeNode.data);
        $("#errMessage").text("");
        if(playMode==0){
            startPreview(treeNode.data,-1);
        }

    }
};
//监控页监控列表
function camaraTree(){
    $.get( "../../jk/jkcamara/indexsCamaraTree",function(r){
        ztree = $.fn.zTree.init($("#indexsCamaraTree"), setting,r.indexsTree);
        var nodes=ztree.transformToArray(ztree.getNodes());
        for(var i=0;i<nodes.length;i++){
            if(nodes[i].level==0){
                ztree.expandNode(nodes[i], true, true, true);
            }else{
                ztree.expandNode(nodes[i], false, true, true);
            }
        }
    })
}

function startPreview(cameraIndexCode,wndId){
    var streamMode = 0;                                     //主子码流标识：0-主码流，1-子码流
    var transMode = 1;                                      //传输协议：0-UDP，1-TCP
    var gpuMode = 0;                                        //是否启用GPU硬解，0-不启用，1-启用
    cameraIndexCode = cameraIndexCode.replace(/(^\s*)/g, "");
    cameraIndexCode = cameraIndexCode.replace(/(\s*$)/g, "");
    oWebControl.JS_RequestInterface({
        funcName: "startPreview",
        argument: JSON.stringify({
            cameraIndexCode:cameraIndexCode,                //监控点编号
            streamMode: streamMode,                         //主子码流标识
            transMode: transMode,                           //传输协议
            gpuMode: gpuMode,                               //是否开启GPU硬解
            wndId:wndId                                     //可指定播放窗口
        })
    })
}

// 录像回放
$("#startPlayback").click(function () {
    var cameraIndexCode  = $("#cName").attr("name");

    if(cameraIndexCode=="" ||cameraIndexCode==undefined){
        $("#errMessage").text("请先选择监控点！");
    }else {
        var startTimeStamp = new Date($("#beginTime").val().replace('-', '/').replace('-', '/')).getTime();
        var endTimeStamp = new Date($("#endTime").val().replace('-', '/').replace('-', '/')).getTime();
        var recordLocation = 0; // 录像存储位置 0中心存储 1设备存储
        var transMode = 1; //传输协议 1TCP 2UDP
        var gpuMode = 0; //GPU硬解 0不启用 1启用
        var wndId = -1;  //默认为选中窗口回放
        var isDirectEzviz = 0;//萤石回放不启用
        if (!cameraIndexCode) {
            alert("监控点编号不能为空！");
            return
        }
        if (!Number.isNaN) {
            Number.isNaN = function(n) {
                return (
                    typeof n === "number" &&
                    window.isNaN( n )
                );
            };
        }
        if (Number.isNaN(+startTimeStamp) || Number.isNaN(+endTimeStamp)) {
            alert("时间格式有误！");
            return
        }

        oWebControl.JS_RequestInterface({
            funcName: "startPlayback",
            argument: JSON.stringify({
                cameraIndexCode: cameraIndexCode,
                startTimeStamp: Math.floor(startTimeStamp / 1000).toString(),
                endTimeStamp: Math.floor(endTimeStamp / 1000).toString(),
                recordLocation: recordLocation,
                transMode: transMode,
                gpuMode: gpuMode,
                wndId: wndId,
                ezvizDirect: isDirectEzviz
            })
        }).then(function (oData) {
            showCBInfo(JSON.stringify(oData ? oData.responseMsg : ''));
        });
    }

})

// 停止回放
$("#stopPlayback").click(function () {
    oWebControl.JS_RequestInterface({
        funcName: "stopAllPlayback"
    }).then(function (oData) {
    });
})
